package com.abbas.smartCity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.abbas.smartCity.App;
import com.abbas.smartCity.R;
import com.abbas.smartCity.bean.ServerBean;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * 列表通用适配器
 * @param <T> 数据类型
 * @param <H> holder持有者
 */
public class ListCommAdapter<T, H> extends BaseAdapter {

    private List<T> data;//数据
    private final Class<H> holderClazz;//持有者类
    private final Integer layoutId;//将要使用的布局ID

    public ListCommAdapter(List<T> data, Class<H> holderClazz, Integer layoutId) {
        this.data = data;
        this.holderClazz = holderClazz;
        this.layoutId = layoutId;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public T getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //没什么区别
        H holder = null;
        if (view == null) {
            try {
                holder = holderClazz.newInstance();//新建出 holder
                view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutId, viewGroup, false);//解析布局
                view.setTag(holder);//设置tag
            } catch (IllegalAccessException | InstantiationException e) {
                e.printStackTrace();
            }
        } else {
            holder = (H) view.getTag();//直接在tag当中拿出持有者
        }
        //根据布局绑定持有者
        onBindView(holder, view);
        //抽象方法，使用时重写决定
        show(data.get(i), view, holder, i, viewGroup.getContext());
        return view;
    }

    /**
     * 根据布局将泛型持有者强转为对应的数据类型，我们可知，布局与持有者是一对一关系。所以转换不会出错
     * 布局与持有者是一对一关系
     * 数据类型与布局是多对一关系
     * @param holder 持有者
     * @param convertView layoutId view
     */
    @SuppressLint("NonConstantResourceId")
    private void onBindView(H holder, View convertView) {
        switch (layoutId) {
            case R.layout.server_item:
                ServerHolder serverHolder = (ServerHolder) holder;
                serverHolder.im = (ImageView) convertView.findViewById(R.id.im);
                serverHolder.tv = (TextView) convertView.findViewById(R.id.tv);
                break;
            case R.layout.news_item:
                NewsHolder newsHolder = (NewsHolder) holder;
                newsHolder.title = (TextView) convertView.findViewById(R.id.title);
                newsHolder.im = (ImageView) convertView.findViewById(R.id.im);
                newsHolder.sub = (TextView) convertView.findViewById(R.id.sub);
                newsHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
                newsHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
                newsHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
                newsHolder.tag4 = (TextView) convertView.findViewById(R.id.tag4);
                break;
            case R.layout.youth_inn_gv_item:
                YouthInnGvHolder youthInnGvHolder = (YouthInnGvHolder) holder;
                youthInnGvHolder.im = (ImageView) convertView.findViewById(R.id.im);
                break;
            case R.layout.talent_policy_item:
                TalentPolicyHolder talentPolicyHolder = (TalentPolicyHolder) holder;
                talentPolicyHolder.name = (TextView) convertView.findViewById(R.id.name);
                talentPolicyHolder.sub = (TextView) convertView.findViewById(R.id.sub);
                break;
            case R.layout.parking_history_item:
                ParkingHistoryHolder parkingHistoryHolder = (ParkingHistoryHolder) holder;
                parkingHistoryHolder.name = (TextView) convertView.findViewById(R.id.name);
                parkingHistoryHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
                parkingHistoryHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
                parkingHistoryHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
                parkingHistoryHolder.money = (TextView) convertView.findViewById(R.id.money);
                break;
            case android.R.layout.simple_list_item_1:
                TextHolder textHolder = (TextHolder) holder;
                textHolder.tv = convertView.findViewById(android.R.id.text1);
                break;
        }
    }

    /**
     * 重置数据
     * @param data 数据
     */
    public void resetData(List<T> data){
        this.data = data;
        notifyDataSetChanged();
    }

    //添加数据
    public void append(List<T> data){
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    /**
     * 抽象方法，由使用场景具体决定
     * @param data 数据
     * @param convertView 视图
     * @param viewHolder 持有者
     * @param position 位置
     * @param context 上下文
     */
    @SuppressLint("NonConstantResourceId")
    protected void show(T data, View convertView, H viewHolder, int position, Context context){
        switch (layoutId){
            case R.layout.server_item:
                if (data instanceof ServerBean.RowsBean){
                    ServerHolder serverHolder = (ServerHolder) viewHolder;
                    ServerBean.RowsBean serverData = (ServerBean.RowsBean) data;
                    Glide.with(context)
                            .load(App.getBaseUrl() + serverData.getImgUrl())
                            .into(serverHolder.im);
                    serverHolder.tv.setText(serverData.getServiceName());
                }else {
                    //throw new RuntimeException("Class cast exception:");
                }
                break;
        }
    }

    /**
     * 全部服务持有者
     */
    public static class ServerHolder {
        public ImageView im;
        public TextView tv;
    }

    /**
     * 新闻持有者
     */
    public static class NewsHolder{
        public TextView title;
        public ImageView im;
        public TextView sub;
        public TextView tag1;
        public TextView tag2;
        public TextView tag3;
        public TextView tag4;
    }

    /**
     * 青年驿站区域列表持有者
     */
    public static class YouthInnGvHolder{
        public ImageView im;
    }

    /**
     * 青年驿站不知道什么持有者
     */
    public static class TalentPolicyHolder{
        public TextView name;
        public TextView sub;
    }

    public static class ParkingHistoryHolder{
        public TextView name;
        public TextView tag1;
        public TextView tag2;
        public TextView tag3;
        public TextView money;
    }

    public static class TextHolder{
        public TextView tv;
    }
}
